#include "../includes.h"

char SyncLEDcnt = 0;

//char GPS_Rx_TimeOut = 0;
//char CheckOutGPSTimeOut = 0;
//unsigned char GPS_Rx_Buf[GPSRXBUFSIZE];
unsigned long timer1_cnt_debug = 0;
int kk=0;

void _TIMER1_Handler(int parameter)
{
	int t;
	
//printf("_TIMER1_Handler[%d]\n",TimeSliceCounter);
	
	timer1_cnt_debug++; //kang:test

	
	TimeSliceCounter++;

	dbgCV(131) printf("TimeSliceCounter : %d\n",TimeSliceCounter);
	//kang 08-12-01
	if(TimeSliceCounter > 102) {
		TimeSliceCounter = 0;
		//printf("_TIMER1_Handler->TimeSliceCounter=0\n");
		//printf("$ 2$");
	}
		
   	TASK.T10ms = ON;
   	VME.T10ms = ON;
	TASK.C10ms++;
	TASK.C100ms++;

	TASK.C10msLED++;

	buf_sync15.Rx_TimeOut++;
	buf_f1000c.Rx_TimeOut++;
	buf_oemv1.Rx_TimeOut++;
	buf_RbSRO.Rx_TimeOut++;

//    if( GPS_Rx_TimeOut >  3 ){ GPS_Rx_TimeOut =0; CheckOutGPSTimeOut = 1; }

    if( buf_sync15.Rx_TimeOut >  3 ){ buf_sync15.Rx_TimeOut =0; buf_sync15.CheckOutTimeOut = 1; }
    if( buf_f1000c.Rx_TimeOut >  3 ){ buf_f1000c.Rx_TimeOut =0; buf_f1000c.CheckOutTimeOut = 1; }
    if( buf_oemv1.Rx_TimeOut >  3 ){ buf_oemv1.Rx_TimeOut =0; buf_oemv1.CheckOutTimeOut = 1; }
    if( buf_RbSRO.Rx_TimeOut >  3 ){ buf_RbSRO.Rx_TimeOut =0; buf_RbSRO.CheckOutTimeOut = 1; }


	if(TASK.C100ms>9){
		TASK.C100ms = 0;
		TASK.T100ms = ON;
	}
	
		
    switch(TimeSliceCounter){

		//------------- TASK -------------------------------
		case 1:
					TASK.T500ms = ON;
					TASK.T1000ms = ON;
					VME.T1000ms = ON;
    				TASK.C10ms = TASK.C500ms = 0;

    				buf_sync15.Rx_Match_TimeOut++;
    				buf_f1000c.Rx_Match_TimeOut++;
    				buf_oemv1.Rx_Match_TimeOut++;
   				buf_RbSRO.Rx_Match_TimeOut++;
					//AutoSendSet();
					//func.autoreply_task = ON;
					
    				break;
    	case 51:
					TASK.T500ms = ON;    	TASK.C500ms++;
					
    				break;
		//------------- TASK -------------------------------


        case  3:
					if(SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){
						func.startup_task = ON;
					}
					if(CGM_P()){
						func.rb_task = ON;
					}
                    break;
		
		case 9:
		case 59:
					EVENT.SCC3 = ON;
					break;

        case 10:
                    if(EVENT.OnePPS1){
                        EVENT.OnePPS1 = OFF;
                        func.control_task = ON;
                    }

				    CONTROL.PWMhit = 0;
                    break;

		//---------------------------------------------
        case 28:
        			if(EVENT.PLL){
        				func.pll_task_1 = ON;
        			}
                    break;
        case 70:
        			if(EVENT.SCC2){
        				EVENT.SCC2 = OFF;
        				func.scc2_task = ON;
        			}
                    break;
		//---------------------------------------------

		case 40:
					if(!(SYSTEM.TIME.CUR.SECOND&0x01)){

                    	func.monitor_cnt_task = ON;
                	}
                	
                	//hmlee - next tod is even second and time valid
                	if(!(SYSTEM.TIME.TOUT.SECOND&0x01) && (SYSTEM.TIME.Valid)){
                		func.fpga_tod_task = ON;
                	}
					break;

		//case 72:
		//
		//			break;
    	//case 80:
    	case 72:
    				func.diag_task = ON;
   
    				break;
        case 73:
        			if(EVENT.PLL){
        				func.pll_task_2 = ON;
        			}
					break;
		case 74:
                    if(TRACE.EVENT){
						func.start_trace_task = ON;
                    }
                    break;

		case 26:
					func.e1_task = ON;
					break;
		//case 76:	
		case 71:	
					func.eb_task = ON;
					break;
        //case 81:
        case 76:
					func.eb_event_task = ON;
					//if(CONFIG.RcvType)func.check_lock_task = ON;
					func.check_lock_task = ON;
                    break;
		//case 82:
		case 77:
                    TOD_MAKE_TASK = ON;
                    break;

		//case 85:
		case 80:
					func.config_log_task = ON;
					break;
		case 94:
					func.counter_shift_task = ON;
					//if(!CONFIG.RcvType)func.check_lock_task = ON;
					break;
		//case 98:
		case 78:
				    PWM();
					if( MANUAL_START == ON ){
						func.manual_start_task = ON;
					}
					break;

		case 99:	//if( !((SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP) && (startup.MAINSTEP != STARTUP_SET))){
					if( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){

					}
					break;

		//------------- LED -------------------------------
		case	2:
					//SyncLEDcnt++;
					//if(SyncLEDcnt == 5){
					//	SyncLEDcnt = 0;
					//	SyncLEDDisplay();
					//}
					func.led_task = ON;
					break;
		case   25:
		case   50:
		case   75:
					func.led_blink_task = ON;
                    func.parameter = TimeSliceCounter;
                    TASK.T4ms = ON;
                    break;
		//------------- LED --------------------------------

		case	5:
					func.is_over10e6_task = ON;
					//kang AutoSendSet();
					func.autoreply_task = ON;
					break;

    }



	if( SYSTEM.ID == ModuleA){
		if(	TimeSliceCounter == 1 || TimeSliceCounter == 51 ){

			if( SelMonitorStatusDebug == 0 ){
				MonitorSTATUS();
			}
			else{
				MyMonitorSTATUSForDebug();
			}
		}
	}

	else {
		if(	TimeSliceCounter == 25 || TimeSliceCounter == 75 ){

			if( SelMonitorStatusDebug == 0 ){
				MonitorSTATUS();
			}
			else{
				MyMonitorSTATUSForDebug();
			}
		}
	}


	func.common_port_task = ON;


	//if(!(TimeSliceCounter%4) ){
	//	TASK.T4ms = ON;    	TASK.C4ms++;
	//}

	if(CONFIG.beep_flag){
		if(beep_alarm_flag){
			if(beep_alarm_counter == 10){

			}
			else if(beep_alarm_counter >= 20){
				beep_alarm_counter = 0;

			}
			beep_alarm_counter++;
		}

		if(beep_pps_flag){
			if(beep_pps_counter++ >= 10 ){
				beep_pps_counter = 0;
				beep_pps_flag = OFF;
			}
		}
	}
	else {

	
	}
	
	//kang AutoSendODTCommand();
	
	//kang t=TSR1;



}


extern unsigned char GPS_Tx_Buf[GPSTXBUFSIZE];
extern char GPSTxHead;
extern char GPSTxTail;
extern char GPS_Tx_Ready;


